﻿using NfsBLL;
using NfsCommon.Util;
using NfsModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MobileWeb.Controllers
{
    [BaseController(IsCheck=false)]
    public class ShareController : Controller
    {
        public ActionResult Index()
        {
            if (CookieHelper.GetValue("nfs_UserId") == null || CookieHelper.GetValue("nfs_UserId") == "")
            {
                ViewData["result"] = "1";
            }
            else {

                ViewData["result"] = "0";
            }
            string url = Request.Url.AbsoluteUri;
            string createNonceStr = Request["nonceStr"] != "" ? Request["nonceStr"] : "";
            ViewData["nonceStr"] = createNonceStr;
            ViewData["flags"] ="0";
            ViewData["id"] = 0;
            ViewData["hours"] = 0;
            ShareBLL bll = new ShareBLL();
            share model = bll.GetModel_nonceStr(createNonceStr);
            if (model != null)
            {
                AgencyBLL agbll = new AgencyBLL();
                int id =(int)model.parent_id;
                agency agmodel = agbll.GetModel_account(id);
                ViewData["username"] = agmodel != null ? agmodel.name : "";
                ViewData["id"] = model.id;
                ViewData["level_name"] = model.level_name;
                DateTime d1 = DateTime.Now;
                DateTime d2 =DateTime.Parse(model.time.ToString()).AddHours(2);
                //TimeSpan ts = d2.Subtract(d1).Duration();
                int housr =(d2-d1).Hours;
                int mins =(d2-d1).Minutes;
                int secons = (d2 - d1).Seconds;
                int miao = (housr * 3600) + (mins * 60) + secons;
                ViewData["hours"] = miao;
                if (housr<=0)
               {
                   ViewData["flags"] = "10";//表示该链接已经过期
                   return View();
               }
               if (model.state != 0)
               {
                   ViewData["flags"] = "20";//表示该链接已经用过了
                   return View();
               }

            }
            else {
                ViewData["flags"] ="100";//表示该链接不存在
                return View();
            }
            ViewData["flags"] = "0";//表示该链接已经过期

            return View();

        }
        #region 修改和新增用户，以及授权
        [HttpPost]
        public JsonResult OpeaAgency()
        {
            try
            {
                bool flag = true;
                int id = Request["id"] != null ? int.Parse(Request["id"].ToString()) : 0;
                int sh_id = Request["sh_id"] != null ? int.Parse(Request["sh_id"].ToString()) : 0;
                string Name = Request["Name"] != null ? Request["Name"] : "";
                string companyName = Request["companyName"] != null ? Request["companyName"] : "";
                string contract_name = Request["contract_name"] != null ? Request["contract_name"] : "";
                string mobile = Request["mobile"] != null ? Request["mobile"] : "";
                string qq = Request["qq_code"] != null ? Request["qq_code"] : "";
                string wxOpenid = Request["wxOpenid"] != null ? Request["wxOpenid"] : "";
                string wwCode = Request["wwCode"] != null ? Request["wwCode"] : "";
                string idCard = Request["idCard"] != null ? Request["idCard"] : "";
                string shopUrl = Request["shopUrl"] != null ? Request["shopUrl"] : "";
                ShareBLL sh_bll = new ShareBLL();
                share sh_model = sh_bll.GetModel(sh_id);
                AgencyBLL bll = new AgencyBLL();
                if (!bll.GetSingle(id, Name, mobile, qq, wxOpenid, wwCode, idCard, shopUrl))
                {
                    agency model = new agency();
                    model.id = id;
                    model.account_id = id;
                    model.parents_id = sh_model.parent_id;
                    model.name = Name;
                    model.company_name = companyName;
                    model.contract_name = contract_name;
                    model.mobile = mobile;
                    model.qq = qq;
                    model.wx_openid = wxOpenid;
                    model.ww_code = wwCode;
                    model.id_card = idCard;
                    model.shop_url = shopUrl;
                    model.state = 0;
                    model.valid = 1;
                    model.level_id = sh_model.level_id;
                    model.category_id = sh_model.category_id;
                    model.level_name = sh_model.level_name;
                    model.shState = 0;//表示未审核
                    account act_model = new account { user_name = mobile, user_pwd = mobile.Substring(5, 6), type = 2, IsAdmin = 0, mobile = mobile, valid = 1, time = DateTime.Now };
                    int agency_id;
                    flag = bll.InsertAgency(model, act_model, out agency_id);
                    if (flag)
                    {
                        share sh_models = new share {id=sh_id,agency_id=agency_id, state =1 };
                       flag=sh_bll.Update(sh_models);
                       if (flag)
                       {
                           return Json(new { MessageCode = "0" }, JsonRequestBehavior.AllowGet);
                       }
                       else {
                           return Json(new { MessageCode = "100" }, JsonRequestBehavior.AllowGet);

                       }

                    }
                    else
                    {
                        return Json(new { MessageCode = "100" }, JsonRequestBehavior.AllowGet);


                    }
                }
                else {
                    return Json(new { MessageCode = "50" }, JsonRequestBehavior.AllowGet);

                }
           

            }
            catch (Exception)
            {

                return Json(new { MessageCode = "200" }, JsonRequestBehavior.AllowGet);
            }

        }
        #endregion

	}
}